package cc.yugu.yhdw.webserver.service.basic;

import cc.yugu.yhdw.common.domain.basic.OrderId;
import cc.yugu.yhdw.webserver.persistence.basic.OrderIdMapper;
import org.apache.commons.lang.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.GregorianCalendar;
/**
 * 订单业务（往不同表插入主键）
 * 
 * @Date:Created in 2018/7/3 17:27
 * @Author:
 * @Modified By: zjn 2018/7/3 17:27
 */ 
@Service
public class OrderIdService {
    @Autowired
    OrderIdMapper orderIdMapper;
    /**
    * 根据不同的订单id往不同表中插入数据（表名随着时间而变化）,并获取拼接后的订单号
    * 
    * @Param: [type]
    * @Return: java.lang.String
    * @Author By: zjn 2018/7/3  17:27
    */ 
    public String newOrderId(OrderId.OrderIdType type) {
        // 当前时刻
        Calendar calendar = new GregorianCalendar();
        // 获取当前时刻的年份
        int suffix = calendar.get(Calendar.YEAR);
        OrderId orderId = new OrderId(type, suffix);
        // type类型不同，就是往不同的表插入数据
        orderIdMapper.insert(orderId);

        String result = null;
        // 如果是 商品
        if(OrderId.OrderIdType.ITEM == type) {
            // 替换， 获取订单号字符串
            String orderNum = String.format(OrderId.NUMBER_FORMAT, orderId.getId());
            if(orderNum.length() > OrderId.NUMBER_LENGTH) {
                throw new IllegalArgumentException("orderId溢出(" + orderNum +")");
            }
            //拼接订单号， CT + 2018 + orderNum
            result = OrderId.PREFIX_ITEM_ORDER + DateFormatUtils.format(calendar, OrderId.DATE_FORMAT) + orderNum;
          //如果是换电
        } else if(OrderId.OrderIdType.EXCHANGE == type) {
            String orderNum = String.format(OrderId.NUMBER_FORMAT, orderId.getId());
            if(orderNum.length() > OrderId.NUMBER_LENGTH) {
                throw new IllegalArgumentException("orderId溢出(" + orderNum +")");
            }

            result = OrderId.PREFIX_EX_ORDER + DateFormatUtils.format(calendar, OrderId.DATE_FORMAT) + orderNum;
          // 充值
        } else if(OrderId.OrderIdType.CHARGE == type) {
            String orderNum = String.format(OrderId.NUMBER_FORMAT, orderId.getId());
            if(orderNum.length() > OrderId.NUMBER_LENGTH) {
                throw new IllegalArgumentException("orderId溢出(" + orderNum +")");
            }

            result = OrderId.PREFIX_CHARGER + DateFormatUtils.format(calendar, OrderId.DATE_FORMAT) + orderNum;
          // 充值
        } else if(OrderId.OrderIdType.DEPOSIT == type) {
            String orderNum = String.format(OrderId.NUMBER_FORMAT, orderId.getId());
            if(orderNum.length() > OrderId.NUMBER_LENGTH) {
                throw new IllegalArgumentException("orderId溢出(" + orderNum +")");
            }

            result = OrderId.PREFIX_DEPOSIT + DateFormatUtils.format(calendar, OrderId.DATE_FORMAT) + orderNum;
          // 如果是支付宝订单
        } else if(OrderId.OrderIdType.ALIPAY_PAY == type) {
            String orderNum = String.format(OrderId.PAY_ORDER_NUMBER_FORMAT, orderId.getId());
            if(orderNum.length() > OrderId.PAY_ORDER_NUMBER_LENGTH) {
                throw new IllegalArgumentException("orderId溢出(" + orderNum +")");
            }

            result = OrderId.PREFIX_ALIPAY_PAY + DateFormatUtils.format(calendar, OrderId.PAY_ORDER_DATE_FORMAT) + orderNum;
          // 如果是微信订单
        } else if(OrderId.OrderIdType.WEIXIN_PAY == type) {
            String orderNum = String.format(OrderId.PAY_ORDER_NUMBER_FORMAT, orderId.getId());
            if(orderNum.length() > OrderId.PAY_ORDER_NUMBER_LENGTH) {
                throw new IllegalArgumentException("orderId溢出(" + orderNum +")");
            }

            result = OrderId.PREFIX_WEIXIN_PAY + DateFormatUtils.format(calendar, OrderId.PAY_ORDER_DATE_FORMAT) + orderNum;

        } else {
            throw new IllegalArgumentException();
        }

        return result;
    }


}
